using System;
using System.Collections.Generic;
using System.Text;
using MCMLXVII.BackEnd.Data.SystemDatabase.DataSets;
using Microsoft.ApplicationBlocks.Data;

namespace MCMLXVII.BackEnd.Data.SystemDatabase
{
    public static class ToolsAndPermissions
    {
        #region Tools

        public static DSTools.ToolsDataTable GetTools()
        {            
            DSTools.ToolsDataTable dt = new DSTools.ToolsDataTable();
            dt.Load(SqlHelper.ExecuteReader(System.Data.CommandType.Text, "SELECT ToolID, ToolName FROM dbo.Tools order by ToolName"));
            if (dt.Rows.Count > 0)
                return dt;
            else
                return null;
        }

        public static DSTools.ToolsRow GetTool( string ToolName)
        {
            DSTools.ToolsDataTable dt = new DSTools.ToolsDataTable();
            dt.Load(SqlHelper.ExecuteReader("dbo.GetToolByName", new object[] {ToolName}));
            if (dt.Rows.Count > 0)
                return (DSTools.ToolsRow)dt[0];
            else
                return null;
        }
        public static DSTools.ToolsRow GetTool( int ToolID)
        {
            DSTools.ToolsDataTable dt = new DSTools.ToolsDataTable();
            dt.Load(SqlHelper.ExecuteReader("dbo.GetToolByID", new object[] {ToolID}));
            if (dt.Rows.Count > 0)
                return (DSTools.ToolsRow)dt[0];
            else
                return null;
        }

        public static DSTools.ToolsPermissionsWithDescDataTable GetToolPermissions(int ToolID)
        {
            DSTools.ToolsPermissionsWithDescDataTable dt = new DSTools.ToolsPermissionsWithDescDataTable();
            dt.Load(SqlHelper.ExecuteReader(System.Data.CommandType.Text, "select TA.PermissionID, TA.Required, P.PermissionName from dbo.ToolsPermissions TA, dbo.Permissions P where TA.ToolID=" + ToolID + " and TA.PermissionID = P.PermissionID"));
            return dt;
        }


        #endregion

        #region Permissions
        public static DSTools.PermissionsDataTable GetPermissions()
        {
            DSTools.PermissionsDataTable dt = new DSTools.PermissionsDataTable();
            dt.Load(SqlHelper.ExecuteReader(System.Data.CommandType.Text, "SELECT PermissionID, PermissionName FROM dbo.Permissions order by PermissionName"));
            if (dt.Rows.Count > 0)
                return dt;
            else
                return null;
        }

        public static DSTools.PermissionsDataTable GetPermissions(int MemberID)
        {
            DSTools.PermissionsDataTable dt = new DSTools.PermissionsDataTable();
            dt.Load(SqlHelper.ExecuteReader(System.Data.CommandType.Text, "SELECT P.PermissionID, P.PermissionName FROM dbo.Permissions P inner join MembersPermissions M on P.PermissionID = M.PermissionID where M.MemberID = " + MemberID.ToString()));
            if (dt.Rows.Count > 0)
                return dt;
            else
                return null;
        }

        public static DSTools.PermissionsDataTable GetInheritedPermissions(int MemberID)
        {
            DSTools.PermissionsDataTable dt = new DSTools.PermissionsDataTable();
            dt.Load(SqlHelper.ExecuteReader(System.Data.CommandType.Text, "SELECT distinct P.PermissionID, P.PermissionName FROM dbo.Permissions P inner join MembersPermissions M on P.PermissionID = M.PermissionID where M.MemberID in (select MemberID from dbo.GetBasicSecurityDescriptor(" + MemberID.ToString() + ", getdate()) where MemberID <> " + MemberID.ToString() + ")"));
            if (dt.Rows.Count > 0)
                return dt;
            else
                return null;
        }

        public static int UpdateMemberPermissions(int MemberID, int UpdatingMemberID, int UpdateSequence, int[] Permissions)
        {

            int res = (int)SqlHelper.ExecuteScalar("dbo.UpdateOrganizationMemberPermissions_A", MemberID, UpdateSequence, UpdatingMemberID);
            foreach (int i in Permissions)
            {
                SqlHelper.ExecuteScalar("dbo.UpdateOrganizationMemberPermissions_B", MemberID, i);
            }
            return res;
        }



        public static DSTools.PermissionsRow GetPermission(string PermissionName)
        {
            DSTools.PermissionsDataTable dt = new DSTools.PermissionsDataTable();
            dt.Load(SqlHelper.ExecuteReader("dbo.GetPermissionByName", new object[] { PermissionName }));
            if (dt.Rows.Count > 0)
                return (DSTools.PermissionsRow)dt[0];
            else
                return null;
        }
        public static DSTools.PermissionsRow GetPermission(int PermissionID)
        {
            DSTools.PermissionsDataTable dt = new DSTools.PermissionsDataTable();
            dt.Load(SqlHelper.ExecuteReader("dbo.GetPermissionByID", new object[] { PermissionID }));
            if (dt.Rows.Count > 0)
                return (DSTools.PermissionsRow)dt[0];
            else
                return null;
        }

        public static DSTools.PermissionToolsWithDescriptionDataTable GetPermissionTools(int PermissionID)
        {
            DSTools.PermissionToolsWithDescriptionDataTable dt = new DSTools.PermissionToolsWithDescriptionDataTable();
            dt.Load(SqlHelper.ExecuteReader(System.Data.CommandType.Text, "select TP.ToolID, TP.Required, T.ToolName from dbo.ToolsPermissions TP, dbo.Tools T where TP.PermissionID=" + PermissionID + " and TP.ToolID = T.ToolID"));
            return dt;
        }

        #endregion
    }
}
